package com.china08.yxyapi.api.manage.sch.platform.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceSchService;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 学校平台办公 资源管理 搜索
 * 
 */
@CrossOrigin
@RestController("SchResourceSrhController")
public class ResourceSrhController extends YxyApiBaseController {

    // @Autowired
    // private YxyResourceService res;

    @Autowired
    private YxyResourceSchService yxyResourceSchSchService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/platform/resource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws IllegalAccessException, InvocationTargetException {
        String schoolId = getProfile(request, response).getSchoolId();
        Integer status = reqModel.status;
        String phase = reqModel.phase;
        String subject = reqModel.subject;
        String keyword = reqModel.keyword;
        Pageable pageable = null;
        if (StringUtils.isNotBlank(keyword)) {
            keyword = keyword.toLowerCase();
            pageable = new PageRequest(reqModel.page, reqModel.size);
            // pageable =
            // reqModel.getPageable(Direction.fromString(reqModel.order),
            // "_score", reqModel.field);
        } else {
            pageable = reqModel.getPageable(Direction.fromString(reqModel.order), reqModel.field);
        }
        List<RespModel> list = new ArrayList<ResourceSrhController.RespModel>();
        long totla = 0l;
        Page<ElasticYxyResResource> page = yxyResourceSchSchService.manageSchPlatform(schoolId, status, phase, subject,
                keyword, pageable);
        totla = page.getTotalElements();
        // ##
        for (ElasticYxyResResource elasticYxyResResource : page) {
            RespModel model = new RespModel();
            BeanUtils.copyProperties(model, elasticYxyResResource);
            model.setUploadTimeFormat(elasticYxyResResource.getUploadTime() != null
                    ? DateToolUtils.format(elasticYxyResResource.getUploadTime(), DateToolUtils.FORMAT_SHORT_YMD) : "");
            model.setAuditDateFormat(
                    DateToolUtils.format(elasticYxyResResource.getLastModifiedDate(), DateToolUtils.FORMAT_SHORT_YMD));
            model.setBookContentNames(elasticYxyResResource.getBookContentNameList());
            model.setExtension(elasticYxyResResource.getExtension());
            list.add(model);
        }
        Page<RespModel> respModel = new PageImpl<ResourceSrhController.RespModel>(list, pageable, totla);
        return respModel;
    }

    public static class RespModel {
        public String title;
        public String typeName;
        public String uploaderSchoolName;
        public String uploaderName;
        public String auditUserName;
        public String auditSchoolName;
        public String uploadTimeFormat;
        public String id;
        public Integer status;
        public String formatId;
        public List<String> bookContentNames;
        public String remark;
        // 文件扩展名
        public String extension;
        // 审核时间（最后一次更改时间）
        public String auditDateFormat;

        public String getExtension() {
            return extension;
        }

        public void setExtension(String extension) {
            this.extension = extension;
        }

        public String getAuditDateFormat() {
            return auditDateFormat;
        }

        public void setAuditDateFormat(String auditDateFormat) {
            this.auditDateFormat = auditDateFormat;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        public List<String> getBookContentNames() {
            return bookContentNames;
        }

        public void setBookContentNames(List<String> bookContentNames) {
            this.bookContentNames = bookContentNames;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getTypeName() {
            return typeName;
        }

        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }

        public String getUploaderSchoolName() {
            return uploaderSchoolName;
        }

        public void setUploaderSchoolName(String uploaderSchoolName) {
            this.uploaderSchoolName = uploaderSchoolName;
        }

        public String getUploaderName() {
            return uploaderName;
        }

        public void setUploaderName(String uploaderName) {
            this.uploaderName = uploaderName;
        }

        public String getAuditUserName() {
            return auditUserName;
        }

        public void setAuditUserName(String auditUserName) {
            this.auditUserName = auditUserName;
        }

        public String getAuditSchoolName() {
            return auditSchoolName;
        }

        public void setAuditSchoolName(String auditSchoolName) {
            this.auditSchoolName = auditSchoolName;
        }

        public String getUploadTimeFormat() {
            return uploadTimeFormat;
        }

        public void setUploadTimeFormat(String uploadTimeFormat) {
            this.uploadTimeFormat = uploadTimeFormat;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public String getFormatId() {
            return formatId;
        }

        public void setFormatId(String formatId) {
            this.formatId = formatId;
        }

    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // - 1 为全部
        public Integer status;

        public String phase;

        public String subject;

        public String keyword;

        // Desc And Asc
        public String order = "desc";

        public String field;
    }
}
